//
//  TestVC.m
//  YeeShopProject
//
//  Created by Yee on 2017/6/19.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "TestVC.h"
#import "TestDesTableCell.h"
#import "TestTypeTableCell.h"
#import "TestTextViewCell.h"
#import "TextFieldTableCell.h"
#import "UIViewController+YWCategoryPicker.h"
#import "UITextPlaceHolderView.h"

@interface TestVC ()<UIActionSheetDelegate>
{

    UIButton  *_nextButton;
    NSString     *_dateString;//时间
    NSString    *_protocoltype;
    
    NSString    *_protocolfanweitype;//协作范围
    
    UITextField  *_textField;
    
    UITextPlaceHolderView *_textView;


}
@end

@implementation TestVC
-(void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self addButtonView];
}
-(void)addNavbarView{
    
    [super addNavbarView];
    self.title=@"发起协议";
}
-(void)addOwnViews{
    
    [super addOwnViews];

    [self registerTableCell];
}
-(void)configOwnViews{
    
    [super configOwnViews];
    
    [self.tableView setFrame:CGRectMake(0, 0, kMainScreenWidth, kcontentViewHeight-45)];
    
    [self configNavigationBarLeftTitle:@"返回"];
}
-(void)addButtonView{
    
   
    _nextButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_nextButton setTitle:@"提交" forState:UIControlStateNormal];
    _nextButton.layer.masksToBounds=YES;
    _nextButton.layer.cornerRadius=5.0f;
    [_nextButton setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:_nextButton];
    [_nextButton setFrame:CGRectMake(20, kcontentViewHeight-45, kMainScreenWidth-40, 40)];
    [self.view addSubview:_nextButton];
    
    [_nextButton addTarget:self action:@selector(postNetData) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)registerTableCell{
    
    
    [self.tableView registerClass:[TestDesTableCell class] forCellReuseIdentifier:[TestDesTableCell cellIdentifier]];
    
    
    [self.tableView registerClass:[TestTextViewCell class] forCellReuseIdentifier:[TestTextViewCell cellIdentifier]];
    
    
    [self.tableView registerClass:[TextFieldTableCell class] forCellReuseIdentifier:[TextFieldTableCell cellIdentifier]];
    


    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row==0) {
        
        TestDesTableCell *cell=[tableView dequeueReusableCellWithIdentifier:[TestDesTableCell cellIdentifier] forIndexPath:indexPath];
        
        return cell;
    }else if(indexPath.row==5)  {
        
        TextFieldTableCell *cell=[tableView dequeueReusableCellWithIdentifier:[TextFieldTableCell cellIdentifier] forIndexPath:indexPath];
        cell.textLabel.text=@"协议名称";
        _textField=cell.textField;
        return cell;
    }else if(indexPath.row==6)  {
        
         TestTextViewCell *cell=[tableView dequeueReusableCellWithIdentifier:[TestTextViewCell cellIdentifier] forIndexPath:indexPath];
        
        return cell;
    }else  {
        
        
        TestTypeTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TestTypeTableCell"];
        
        if (cell==nil) {
            
            cell=[[TestTypeTableCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"TestTypeTableCell"];
        }
        [cell configTableView:tableView withindexPath:indexPath];
        
        if (indexPath.row==1) {
            
            cell.detailTextLabel.text=_dateString;
        }else if (indexPath.row==2){
            if (_protocoltype.length>0) {
                
                if ([_protocoltype isEqualToString:@"1"]) {
                  
                    cell.detailTextLabel.text=@"服务";
                }else{
                    cell.detailTextLabel.text=@"日常";

                }
            }
        }else if (indexPath.row==3){//协作范围
            
            if ([_protocoltype isEqualToString:@"1"]) {
                
                cell.detailTextLabel.text=@"个人";
            }else{
                
                if ([_protocolfanweitype isEqualToString:@"1"]) {
                    
                    cell.detailTextLabel.text=@"门店";

                }else if([_protocolfanweitype isEqualToString:@"2"]){
                    cell.detailTextLabel.text=@"部门";

                    
                }
                
            }
            
        }
        return cell;
    }
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] init];
    return view;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 0.01)];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==6) {
        
        return 200;
    }
    return 46;
}
#pragma mark --点击单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==1) {//选择日期
        
        [self  viewControllerPOPPickerType:YWCategoryPickerAgeType withsuccessBlock:^(NSString *string)
         {
             
             _dateString=string;
             //两个都要刷新
             [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
             
             
         }];
        
    }else if (indexPath.row==2){
        
        [self callPickerActionSheet];
    
    }else if (indexPath.row==3){
        
        
        if ([_protocoltype isEqualToString:@"1"]) {//选择了服务不操作
            
        }else{//选择了日常继续操作
            
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"门店", @"部门", nil];
            actionSheet.cancelButtonIndex = 2;
            [actionSheet showInView:self.view];
            actionSheet.tag=10000;
            
        }
    }
    
}
- (void)callPickerActionSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"服务", @"日常", nil];
    actionSheet.cancelButtonIndex = 2;
    [actionSheet showInView:self.view];
}
//在这里需要提醒用户去开启相册和相机
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == actionSheet.cancelButtonIndex)
    {
        return;
    }else{
        
        
        if (actionSheet.tag==10000) {
            if (buttonIndex==0) {//门店
                
                _protocolfanweitype=@"1";
            }else if (buttonIndex==1){//日常
                
                _protocolfanweitype=@"2";//部门
                
            }
            
             [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
        }else{
            
            if (buttonIndex==0) {//服务
                
                _protocoltype=@"1";
            }else if (buttonIndex==1){//日常
                
                _protocoltype=@"2";
                
            }
            [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
            [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
            
        }
       
    }

}
-(void)postNetData{
    
    if (_textField.text.length>10) {
        [self.view showNBErrorMessage:@"大于10"];
        return ;
    }

    NSLog(@"%@%@%@%@",_dateString,_protocoltype,_protocolfanweitype,_textField.text);
    
}
//left事件处理
- (void)navigationBarLeftButtonTapped:(UIButton *)sender
{

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"门店", @"部门", nil];
    actionSheet.cancelButtonIndex = 2;
    [actionSheet showInView:self.view];

    
    
}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
